package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.MallActivityDetails;
import com.ktgj.customer.domain.vo.MallActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.MallActivity;
import com.ktgj.customer.service.IMallActivityService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/activity")
public class MallActivityController extends BaseController
{
    @Autowired
    private IMallActivityService mallActivityService;

    /**
     * 查询活动列表
     */
    @PreAuthorize(hasPermi = "customer:activity:list")
    @GetMapping("/list")
    public TableDataInfo list(MallActivity mallActivity)
    {
        startPage();
        List<MallActivity> list = mallActivityService.selectMallActivityList(mallActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize(hasPermi = "customer:activity:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallActivity mallActivity) throws IOException
    {
        List<MallActivity> list = mallActivityService.selectMallActivityList(mallActivity);
        ExcelUtil<MallActivity> util = new ExcelUtil<MallActivity>(MallActivity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize(hasPermi = "customer:activity:query")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId)
    {
        return AjaxResult.success(mallActivityService.selectMallActivityById(activityId));
    }

    /**
     * 新增活动
     */
    @PreAuthorize(hasPermi = "customer:activity:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallActivity mallActivity)
    {
        return toAjax(mallActivityService.insertMallActivity(mallActivity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize(hasPermi = "customer:activity:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallActivity mallActivity)
    {
        return toAjax(mallActivityService.updateMallActivity(mallActivity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize(hasPermi = "customer:activity:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds)
    {
        return toAjax(mallActivityService.deleteMallActivityByIds(activityIds));
    }

    /**
     * 后台活动列表
     * @param map
     * @return
     */
    @PostMapping("/activityList")
    public TableDataInfo activityList(@RequestBody Map<String, Object> map) {
        startPage();
        Long typeId = Long.valueOf(map.get("typeId").toString());
        String activityName = String.valueOf(map.get("activityName"));
        List<MallActivityVO> mallActivityVOS = mallActivityService.activityList(typeId, activityName);
        return getDataTable(mallActivityVOS);
    }

    /**
     * 后台活动详情
     * @param map
     * @return
     */
    @PostMapping("/activityDetails")
    public AjaxResult activityDetails(@RequestBody Map<String, Long> map) {
        Long activityId = map.get("activityId");
        MallActivityDetails activityDetails = mallActivityService.activityDetails(activityId);
        return AjaxResult.success(activityDetails);
    }

    /**
     * 后台修改活动详情
     * @param activityDetails
     * @return
     */
    @PostMapping("/updateActivityDetails")
    public AjaxResult updateActivityDetails(@RequestBody MallActivityDetails activityDetails) {
        return toAjax(mallActivityService.updateActivityDetails(activityDetails));
    }

    /**
     * 后台添加活动
     * @param activityDetails
     * @return
     */
    @PostMapping("/addActivityDetails")
    public AjaxResult addActivityDetails(@RequestBody MallActivityDetails activityDetails) {
        return toAjax(mallActivityService.addActivityDetails(activityDetails));
    }

    /**
     * 后台删除活动
     * @param map
     * @return
     */
    @PostMapping("/deleteActivityDetails")
    public AjaxResult deleteActivityDetails(@RequestBody Map<String, Long> map) {
        Long activityId = map.get("activityId");
        return toAjax(mallActivityService.deleteActivityDetails(activityId));
    }
}
